﻿using System;
using System.Windows.Forms;

namespace MissionImpossible
{
  public partial class AddPeopleDialog : Form
  {
    public PeopleData EnteredData { get; private set; }

    public AddPeopleDialog()
    {
      InitializeComponent();
      SetupHelp();
      SetUpErrorReporting();
    }

    private void SetUpErrorReporting()
    {
      textBox1.Validated += textBox1_Validated;
      textBox2.Validated += textBox2_Validated;
    }

    void textBox2_Validated(object sender, EventArgs e)
    {
      if(textBox2.Text.Trim() == string.Empty)
      {
        errorProvider1.SetError(textBox2, "Surname can not be empty");
      }
      else
      {
        errorProvider1.SetError(textBox2, "");
      }
    }

    void textBox1_Validated(object sender, EventArgs e)
    {
      if (textBox1.Text.Trim() == string.Empty)
      {
        errorProvider1.SetError(textBox1, "Name can not be empty");
      }
      else
      {
        errorProvider1.SetError(textBox1, "");
      }
    }

    private void SetupHelp()
    {
      helpProvider1.SetHelpString(textBox1, "Enter people name");
      helpProvider1.SetHelpString(textBox2, "Enter people last name");
      helpProvider1.SetHelpString(dateTimePicker1, "Select people birth date");
      helpProvider1.SetShowHelp(textBox1, true);
      helpProvider1.SetShowHelp(textBox2, true);
      helpProvider1.SetShowHelp(dateTimePicker1, true);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      EnteredData = new PeopleData()
                      {
                        Name = textBox1.Text.Trim(),
                        Lastname = textBox2.Text.Trim(),
                        Birthdate = dateTimePicker1.Value
                      };
      Close();
    }


    public class PeopleData
    {
      public string Name { get; set; }

      public string Lastname { get; set; }

      public DateTime Birthdate { get; set; }
    }
  }
}
